<?php
require_once('Pick.php');
require_once('functions.php');

/*******************************************************************************************************/
    function getWeighedList($InputID,$Weeknum,$League)
    {
		//Connect To Database
		//dbConnect();
		//check to make sure $Weeknum > 1 AND $Weeknum < 21
		//$Weeknum = checkWeekBounds(getWeek());
		$size = getGameCount($Weeknum,$League);
		$PickList = array();
		$num = 1;
    	for ($i=1; $i<=$size; $i++) 
		{
			$query = "SELECT ".$League."Picks.Game".$i.", ".$League."WeighPicks.Game".$i."Rank, ".$League."Schedule.* FROM ".$League."Picks, ".$League."WeighPicks, ".$League."Schedule WHERE (".$League."Picks.UserID=$InputID AND ".$League."Picks.Week=$Weeknum AND ".$League."WeighPicks.UserID=$InputID AND ".$League."WeighPicks.Week=$Weeknum) AND (".$League."Schedule.Week=$Weeknum AND ".$League."Schedule.GameNum=$i AND ".$League."Schedule.PickEmailed=0) ";
			$result = mysql_query($query) or die("Cannot query the database.<br/>");
			$numrows = mysql_num_rows($result);
			$myrow = mysql_fetch_array($result);
			if($numrows > 0) {
				# 					 Pick($GameNum,$UserPick,$UserID,$Ranking,$Home,$Away,$Winner,$Week,$GameInProgress)
				$PickList[$num] =& new Pick($i,$myrow["Game".$i],$InputID,$myrow["Game".$i."Rank"],$myrow["Home"],$myrow["Away"],$myrow["Winner"],$Weeknum,$myrow["PickEmailed"]);
				$num++;
			}
		}

		//Return Sorted List
        return sortTheList($PickList);         
    }
    
    /*******************************************************************************************************/
    function getUnWeighedList($InputID,$Weeknum,$League)
    {
		//Connect To Database
		//dbConnect();
		//check to make sure $Weeknum > 1 AND $Weeknum < 21
		//$Weeknum = checkWeekBounds(getWeek());
		$size = getGameCount($Weeknum,$League);
		$PickList = array();
		$num = 1;
    	for ($i=1; $i<=$size; $i++) 
		{
			$query = "SELECT ".$League."Picks.Game".$i.", ".$League."WeighPicks.Game".$i."Rank, ".$League."Schedule.* FROM ".$League."Picks, ".$League."WeighPicks, ".$League."Schedule WHERE (".$League."Picks.UserID=$InputID AND ".$League."Picks.Week=$Weeknum) AND (".$League."WeighPicks.UserID=$InputID AND ".$League."WeighPicks.Week=$Weeknum) AND (".$League."Schedule.Week=$Weeknum AND ".$League."Schedule.GameNum=$i AND ".$League."Schedule.PickEmailed=1) ";
			$result = mysql_query($query) or die("Cannot query the database.<br/>");
			$numrows = mysql_num_rows($result);
			$myrow = mysql_fetch_array($result);
			if($numrows > 0) {
				# 					 Pick($GameNum,$UserPick,$UserID,$Ranking,$Home,$Away,$Winner,$Week)
				$PickList[$num] =& new Pick($i,$myrow["Game".$i],$InputID,$myrow["Game".$i."Rank"],$myrow["Home"],$myrow["Away"],$myrow["Winner"],$Weeknum,$myrow["PickEmailed"]);
				$num++;
			}
		}

		//Return Sorted List
        return sortTheList($PickList);               
    }
    
 /*******************************************************************************************************/   
    function sortTheList($List)	//Sort List By Rank
    {
		$size = count($List);
		$missing = 0;#16-$size;
		//Sort List By Rank
    	for ($i=1; $i<$size-$missing; $i++)
    	{
			//echo "<br/>i = $i out of $size ->";
        	for ($j=1; $j<=($size-$i); $j++)
        	{
				//echo " j = $j , ";
             	if ( ($List[$j+1]->getRank()) > ($List[$j]->getRank()) )
				{	// SWAP
					$temp = $List[$j];
					$List[$j] = $List[$j+1];
					$List[$j+1] = $temp;
				}//IF
			}// J Loop
		}// I Loop
				
		return $List;
    }
/*******************************************************************************************************/    
    function processPickOrder($key,$inputID,$League)
    {	
		#$weeknum = checkWeekBounds(getWeek());
		
		/*
        if (!isset($_POST[$key]) || !is_array($_POST[$key]))
			return;
		
        # Highest Rank (first pick) is worth the maximum amount for the week.
		# I.E. 
		#	If there are 16 games this week pick #1 is worth 16 points, pick #16 is worth 1 point.
		#	If there are 14 games this week pick #1 is worth 14 points, pick #14 is worth 1 point.
 		*/
        $ranking = count($key);
        foreach ($key as $GameNum)
        {
            #$SQL = "UPDATE ".$League."Picks SET Game".$GameNum."Rank=".$ranking." WHERE (Week = ".checkWeekBounds(getWeek())." AND UserID=".$inputID.")";
            $SQL = "UPDATE ".$League."Picks SET Game".$GameNum."Rank=".$ranking." WHERE (Week=2 AND UserID=1)";
			echo "$SQL<br/>";
			mysql_query($SQL) or die("Cannot query the database.<br/>");
            $ranking--;
        }
    }
/*******************************************************************************************************/
    /*
    
    function getNameFromID($InputID)
    {
        $query = "SELECT uname FROM wishlist_members WHERE UserID=$InputID";
		$result = mysql_query($query) or die("Cannot query the database.<br/>");
		$num_rows = mysql_num_rows($result);
		$myrow = mysql_fetch_array($result);
		if ($num_rows == 1)
		{
			return $myrow["uname"];
		}
    }
    */
?>